package com.heima.kafka.boot.controller;

import com.alibaba.fastjson.JSON;
import com.heima.kafka.boot.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("kafka")
public class HelloController {

    @Autowired
    KafkaTemplate kafkaTemplate;

    @GetMapping("hello")
    public String hello() {
        //第一个参数：发送主题，目的地
        //第二个参数：发送数据
        User user = User.builder().username("李四").age(18).build();
        //转化为json字符串
        String userJson = JSON.toJSONString(user);
        kafkaTemplate.send("kafka", userJson);
        return "OK";
    }
}